import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token'
// 首页
const Layout = () => import('@/views/Layout')
const Home = () => import('@/views/Layout/Home')
// 用户
const User = () => import('@/views/User/User')
const UserEdit = () => import('@/views/User/UserEdit')
// 登录
const Login = () => import('@/views/Login')
// 文章
const Search = () => import('@/views/article/Search')
const SearchResult = () => import('@/views/article/SearchResult')
const ArticleDetail = () => import('@/views/article/ArticleDetail')
// 聊天
const Chat = () => import('@/views/Chat/Chat')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/layout' },
  {
    path: '/layout',
    component: Layout,
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: Home,
        meta: {
          isRecord: true,
          scrollTop: 0
        }
      },
      { path: 'user', component: User }
    ]
  },
  { path: '/login', component: Login },
  { path: '/search', component: Search },
  { path: '/search/:keywords', component: SearchResult },
  { path: '/detail', component: ArticleDetail },
  { path: '/userEdit', component: UserEdit },
  { path: '/chat', component: Chat }

]

const router = new VueRouter({
  routes
})

// 路由的全局守卫
router.beforeEach((to, from, next) => {
  if (getToken()?.length > 0 && to.path === '/login') {
    next('/')
  } else {
    next()
  }
})
export default router
